package saint.UI.map;

import saint.UI.SmoothCanvas;
import android.content.Context;
import android.graphics.Canvas;
import android.hardware.SensorListener;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

public class RotageView extends ViewGroup implements SensorListener {

	private static final float SQ2 = 1.414213562373095f;
	private final SmoothCanvas mCanvas = new SmoothCanvas();
	private float mHeading = 0;
	
	public RotageView(Context context) {
		super(context);
	}
	
	public RotageView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	public RotageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	
	//============ Override Method ==========================================//
	@Override
	public void dispatchDraw(Canvas canvas){
		canvas.save(Canvas.MATRIX_SAVE_FLAG);
		canvas.rotate(-mHeading, getWidth() * 0.5f, getHeight() * 0.5f);
		mCanvas.delegate = canvas;
		super.dispatchDraw(mCanvas);
		canvas.restore();
	}
	
	@Override
	public void onSensorChanged(int sensor, float[] values) {
		synchronized (this) {
			mHeading = values[0];
			invalidate();
		}
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		final int width = getWidth();
		final int height = getHeight();
		final int count = getChildCount();
		for(int i = 0; i < count; i++) {
			final View view = getChildAt(i);
			final int childWidth = view.getMeasuredWidth();
			final int childHeight = view.getMeasuredHeight();
			final int childLeft = (width - childWidth) / 2;
			final int childTop = (height - childHeight) / 2;
			view.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
		}
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int w = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
		int h = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
		int sizeSpec;
		int count = getChildCount();
		if(w > h) {
			sizeSpec = MeasureSpec.makeMeasureSpec((int)(w * SQ2), MeasureSpec.EXACTLY);
		}
		else {
			sizeSpec = MeasureSpec.makeMeasureSpec((int)(h * SQ2), MeasureSpec.EXACTLY);
		}
		for(int i = 0; i < count; i++) {
			getChildAt(i).measure(sizeSpec, sizeSpec);
		}
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		
	}
	
	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		invalidate();
		return super.dispatchTouchEvent(ev);
	}
	
	@Override
	public void onAccuracyChanged(int sensor, int accuracy) {
		// do not do any thing
	}

}
